#ifndef CONTROLLER_H
#define CONTROLLER_H

#pragma once
#include <QApplication>
#include <QProgressBar>
#include <QPointer>
#include <QStringList>
#include <QDebug>
#include <libkydatacollect.h>

#include "core/capturerequest.h"
#include "widgets/capturewidget.h"
#include "widgets/trayicon.h"
#include "utils/desktopinfo.h"
#include "widgets/leadwidget.h"
#include "widgets/settingwidget.h"
#include "widgets/statusbar.h"
#include "utils/screengrabber.h"
#include "utils/screencapsaver.h"
#include "utils/confighandler.h"
#include "widgets/camerapage.h"
#include "utils/globalkey.h"

class Controller : public QObject
{
    Q_OBJECT
public:
    static Controller *getInstance();
    bool isManagerStart;

    Controller(const Controller &) = delete;
    void operator =(const Controller &) = delete;
    void setPropertyOfSelectWid( uint qId, bool isfullscreen, bool isWindow, QString screen, uint id, QRect rect, double scale);
    void startRecord(uint winId, const QRect &rect);
    void stopRecord(uint qId = 0);
    void pauseRecord();
    void contineRecord();
    void openMicrophone();
    void closeMicrophone();
    void openSettingWid();              //打开设置界面
    void initMicDevices();              //初始化 设置界面的麦克风选项
    void getAllMicDevicesName(QStringList &list );          // 获取麦克风名称
    void slotCloseRec();
    void diskSpaceLow();                //磁盘空间不足

    void initCameraRecorder(bool isShow, QRect cameraQRect = QRect(), QRect recordingRect = QRect());   //录制时使用摄像头
    void closeCameraRecorder();                         //关闭摄像头
    void slotCameraRecordToggled(bool checked);         //从设置界面开启/关闭摄像头
    void CloseCamerafromSetting();

    void hideLeadWid();
signals:
    void sigStartRecording();           //开始录制
    void sigStopRecording();            //结束录制

    void captureTaken(uint id, QString completePath);
    void captureFailed(uint id);
    void captureExit(uint id);

public slots:
    void requestCapture(const CaptureRequest &request);
    void enableTrayIcon();              //创建托盘图标
    void disableTrayIcon();
    void disableScreencap();
    void inputDevice_get(QString str);  //输入设备拔插
    void inputDevice_mute(bool str);    //输入设备静音状态

private slots:
    void slotTrayIconActivated();       //点击托盘图标
    void slotTrayStopRecAndSave();
    void startVisualCapture(const uint id = 0, const QString &forcedSavePath = QString());      //自定义录制
    void startFullscreenCapture(const uint id = 0);                                             //全屏录制
    void startScreenGrab(const uint id = 0, const int screenNumber = -1);                       //窗口录制
    bool diskSpaceDetection();          //存储空间检测
    void micDeviceListDetection();      //麦克风设备列表监测
    void statusMicIcon();

    void handleCaptureTaken(uint id, QString completePath);
    void handleCaptureFailed(uint id);

    void unsetShortcut(ShortcutRec type, QString ShortcutText);
    //快捷键
    void slotStart();       //开始/停止录制
    void slotPause();       //暂停/继续录制
    void slotCustom();      //自定义录制
    void slotFull();        //全屏录制
    void slotWin();         //窗口录制
    void slotOpenMic();     //开启/关闭麦克风

private:
    Controller();
    ~Controller();
    void initConnect();
    void initDBus();
    void initShortcut();
    QList<QKeySequence> parseKeySequence(const QString &keySequenceStr);
    void removeShortcut();
    void checkSavePath();

    QMap<uint, CaptureRequest> m_requestMap;
    QPointer<CaptureWidget> m_captureWindow;    //捕获窗口
    QPointer<TrayIcon> m_trayIcon;              //托盘
    QPointer<LeadWidget> m_leadWid;             //引导窗口
    QPointer<SettingWidget> m_settingWid;       //设置窗口
    QPointer<StatusBar> m_statusBar;            //状态栏
    QPointer<CameraPage> m_camera;              //摄像头
    DesktopInfo m_info;

    QString m_savePath = "";                    //保存文件路径
    QString m_completePath = "";                //录制结束后文件的完整路径
    QPointer<QTimer> m_spaceDetectionTimer;
    QPointer<QTimer> m_micDeviceListTimer;
    bool changedButNoModify = false;

    bool m_diskSpaceLow = false;                //存储空间不足
    QStringList m_micList;                      //麦克风列表
    uint m_qId = 0;

    //快捷键
    GlobalKey * m_shortcutStart = nullptr;
    GlobalKey * m_shortcutPause = nullptr;
    GlobalKey * m_shortcutCustom = nullptr;
    GlobalKey * m_shortcutFull = nullptr;
    GlobalKey * m_shortcutWin = nullptr;
    GlobalKey * m_shortcutOpenMic = nullptr;

    QRect cameraRectLast;

    QString m_NoMic = tr("none mic");
    QString m_usingMicName;

    //摄像头
    QRect m_recordingRect;

    QDBusInterface *m_dbus_media;
    QDBusInterface *m_dbus_interface;
    quint32 m_inhibit_value;             // 阻止锁屏cookie
};

#endif // CONTROLLER_H
